package com.multi.thread.chapter05.demo01;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTest02 {
	
	private static final SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private static Timer timer = new Timer();
	
	static private class TimerTaskTest1 extends TimerTask {
		@Override
		public void run() {
			try {
				System.out.println("1 begin 运行了，时间为:" + ft.format(new Date()));
				Thread.sleep(20000);
				System.out.println("1   end 运行了，时间为:" + ft.format(new Date()));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	static private class TimerTaskTest2 extends TimerTask {
		@Override
		public void run() {
			System.out.println("2 begin 运行了，时间为:" + ft.format(new Date()));
			System.out.println("运行了，时间为:" + ft.format(new Date()));
			System.out.println("2   end 运行了，时间为:" + ft.format(new Date()));
		}
		
	}
	
	public static void main(String[] args) {
		try {
			TimerTaskTest1 task1 = new TimerTaskTest1();
			TimerTaskTest2 task2 = new TimerTaskTest2();
			String date1 = "2018-02-22 13:55:00";
			String date2 = "2018-02-22 13:55:00";
			System.out.println("字符串1时间：" + date1 + ", 当前时间：" + ft.format(new Date()));
			System.out.println("字符串2时间：" + date2 + ", 当前时间：" + ft.format(new Date()));
			timer.schedule(task1, ft.parse(date1));
			timer.schedule(task2, ft.parse(date2));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
